package com.hwn.datax.controller;

import com.hwn.datax.domain.DataxJobRunLog;
import com.hwn.datax.service.DataxJobService;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * @author wnhuang
 * @date 2024/6/5 17:35
 */
@Controller
public class JobRunLogController {

    @FXML
    public HBox progressBox;
    @FXML
    private TextField taskNameField;
    @FXML
    private TextField taskIdField;
    @FXML
    private TextField startTimeField;
    @FXML
    private TextField endTimeField;
    @FXML
    private TextField totalRecordsField;
    @FXML
    private TextArea logContent;
    @FXML
    private ProgressBar progressBar;
    @FXML
    private Label progressLabel;

    private String uuid;

    @Autowired
    private DataxJobService dataxJobService;

    private Boolean isMoveInTextArea = false;

    @FXML
    public void initialize() {
        // 这里可以根据日志文件内容或其他业务逻辑来调整进度条的值
        // 同样可以根据逻辑设置其他字段的值，例如日志内容等
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
        loadData();
    }

    private void setProgressBarClass(int status) {
        // 首先移除所有相关的样式类
        progressBar.getStyleClass().removeAll("progress-bar-blue", "progress-bar-green", "progress-bar-red");
        switch (status) {
            case 0 -> progressBar.getStyleClass().add("progress-bar-blue");
            case 1 -> progressBar.getStyleClass().add("progress-bar-green");
            case 2 -> progressBar.getStyleClass().add("progress-bar-red");
        }
    }

    public void loadData() {
        DataxJobRunLog dataxJobRunLog = dataxJobService.getExecLogByKey(uuid);
        progressBar.progressProperty().bind(dataxJobRunLog.percentageProperty().divide(100)); // 假设当前进度为75%
        taskIdField.setText(uuid);
        taskNameField.setText(dataxJobRunLog.getJobName());
        startTimeField.setText(dataxJobRunLog.getStartTime());
        totalRecordsField.textProperty().bind(dataxJobRunLog.totalRecordsProperty().asString());
        logContent.textProperty().bind(dataxJobRunLog.logContentProperty());
        dataxJobRunLog.logContentProperty().addListener((observable, oldValue, newValue) -> {
            // 获取TextArea的ScrollPane
            ScrollPane scrollPane = (ScrollPane) logContent.lookup(".scroll-pane");
            if (!isMoveInTextArea && scrollPane != null) {
                // 使用Platform.runLater()来恢复之前保存的垂直滚动位置
                Platform.runLater(() -> {
                    //滚动到底部
                    scrollPane.setVvalue(1.0);
                });
            }
        });

        // 设置鼠标进入事件处理器
        logContent.setOnMouseEntered(event -> {
            isMoveInTextArea = true;
        });

        // 设置鼠标离开事件处理器
        logContent.setOnMouseExited(event -> {
            isMoveInTextArea = false;
        });


        dataxJobRunLog.statusProperty().addListener((observable, oldValue, newValue) -> {
            setProgressBarClass(newValue.intValue());
        });
        setProgressBarClass(dataxJobRunLog.getStatus());
        progressLabel.textProperty().bind(
                Bindings.when(dataxJobRunLog.statusProperty().isEqualTo(0))
                        .then(dataxJobRunLog.percentageProperty().asString("%.2f").concat("%"))
                        .otherwise(
                                Bindings.when(dataxJobRunLog.statusProperty().isEqualTo(1))
                                        .then("完成")
                                        .otherwise("错误")
                        )
        );


        endTimeField.textProperty().bind(dataxJobRunLog.endTimeProperty());
    }
}
